package cn.edu.scau.cmi.linyuetao.singleton;

import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;

public class IndexViewController {
    public static int generalNumbers = 0;
    public static String generalMessage = "";

    @FXML
    private Label generalLabel;

    @FXML
    private TextField generalName;

    @FXML
    private Label chairmanLabel;

    @FXML
    private Label successLabel;

    @FXML
    private Label errorLabel;

    @FXML
    private TextField chairmanName;

    @FXML
    public void createChairman() {
        errorLabel.setVisible(false);
        successLabel.setVisible(false);
        Chairman chairman = Chairman.getInstance(chairmanName.getText().toString());
        if (chairman != null) {
            createChairmanSuccessfully(chairman);
        } else {
            createFailed();
        }
    }

    @FXML
    public void createGeneral() {
        errorLabel.setVisible(false);
        successLabel.setVisible(false);
        General general = General.getInstance(generalName.getText().toString());
        if (general != null) {
            General.getGenerals()[generalNumbers++] = general;
            generalMessage += general.getName() + "\n";
            createGeneralSuccessfully();
        } else {
            createFailed();
        }
    }

    private void createGeneralSuccessfully() {
        showGeneralDetails();
        successLabel.setVisible(true);

    }

    private void showGeneralDetails() {
        generalLabel.setText(generalMessage);
        generalLabel.setVisible(true);
    }

    private void createFailed() {
        errorLabel.setVisible(true);
    }

    private void createChairmanSuccessfully(Chairman chairman) {
        showChairmanDetails(chairman);
        successLabel.setVisible(true);
    }



    public Label getSuccessLabel() {
        return successLabel;
    }

    public Label getErrorLabel() {
        return errorLabel;
    }

    private void showChairmanDetails(Chairman chairman) {
        chairmanLabel.setText(chairman.getName());
        chairmanLabel.setVisible(true);
    }
}
